<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/2/002
 * Time: 10:39
 */

namespace app\admin\logic;


use app\common\logic\BaseLogic;
use app\common\model\Lists;
use think\Db;
use think\db\Where;
use think\Exception;

class ListsLogic extends BaseLogic
{

    public function listing($title, $page_size = 100)
    {
        $_where = [];
        !empty($title) && $_where['title'] = ['like', "%$title%"];
        $where = new Where($_where);

        //$r = Lists::where($where)->paginate($page_size)->toArray();
        $field = 'lists.*,cate.name as cate_name';
        $lists_m = new Lists();
        $r = $lists_m
            ->join('cate', 'lists.pid = cate.id')
            ->order('lists.sort', 'desc')
            ->field($field)
            ->where($where)
            ->paginate($page_size)
            ->toArray();
        return $r;
    }

    public function create($data)
    {
        $lists_m = new Lists();

        $data_verify = $lists_m->verify($data);

        if ($data_verify === false) {
            $this->error = $lists_m->getError();
            return false;
        }
        // sort 自加1
        $query = Lists::order('sort', 'desc')->limit(1)->field('sort')->find();
        if ($query['sort'] == 0 || !$query) {
            $data_verify['sort'] = 1;
        } else {
            $data_verify['sort'] = $query['sort'] + 1;
        }

        $lists_m->allowField(true)->save($data_verify);
        return true;
    }

    public function update($data, $methods = 'post')
    {
        if ($methods === 'get') {
            $r = Lists::get($data);
            $r['peizhi'] = str_replace('/',',',$r['peizhi']);
            $r['liangdian'] = str_replace('/',',',$r['liangdian']);
        } else {

            if (empty($data['z_img']))
                unset($data['z_img']);

            if (empty($data['w_img']))
                unset($data['w_img']);

            $lists_m = new Lists();

            $data_verify = $lists_m->verify($data);

            if ($data_verify === false) {
                $this->error = $lists_m->getError();
                return false;
            }

            $lists_m->allowField(true)->update($data_verify);
            return true;
        }
        return $r;
    }

    public function delete($ids)
    {
        $str = trim($ids, ',');
        $_arr = explode(',', $str);
        $where[] = ['id', 'in', $_arr];
        $r = Lists::where($where)->delete();
        return $r;
    }

    public function move($id, $type)
    {
        $query = Lists::get($id);
        if (!$query) {
            $this->error = '参数错误！';
            return false;
        }

        Db::startTrans();
        try {
            if ($type === 'top') {
                $where[] = ['sort', '>', $query['sort']];
                $r = Lists::where($where)->field('id,sort')->order('sort', 'asc')->limit(1)->find();  // 查找最近的上一个
                if ($r['sort'] === 0) {
                    $this->error = '排序类型错误！';
                    return false;
                }

            } elseif ($type === 'down') {
                $where[] = ['sort', '<', $query['sort']];
                $r = Lists::where($where)->field('id,sort')->order('sort', 'desc')->limit(1)->find();  // 查找最近的下一个
                if ($r['sort'] === 0) {
                    $this->error = '排序类型错误！';
                    return false;
                }
            } else {
                $this->error = '类型错误！';
                return false;
            }

            Lists::update(['sort' => $r['sort']], ['id' => $id]);
            Lists::update(['sort' => $query['sort']], ['id' => $r['id']]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
        return true;
    }

    public function many_upload($id, $image)
    {
        if (empty($id)) {
            $this->error = "参数丢失!";
            return false;
        }
        if (!empty($image)) {
            Lists::update(['many_images' => $image], ['id' => $id]);
        }

        return true;
    }
}